Renaming variable with a suffix within an SPSS Macro - spss

I use the following code to winsorize variables in my dataset using SPSS. In the line where it says COMPUTE !var = MAX(MIN(!var, maxval), minval)., I want it to create a new variable with _w as the suffix. Of course, COMPUTE !var_w = MAX(MIN(!var, maxval), minval). will not work. Any solutions, preferably without using Python? Also, is there a way to loop this so that I can include more than one variable when I execute it? For example, !winsor vars = roa, leverage, roe lowpc = 1 hipc = 1..
DEFINE !winsor (var = !TOKENS(1) / lowpc = !TOKENS(1) / hipc = !TOKENS(1))
COMPUTE nobreak=1.
RANK VARIABLES = !var (A) BY nobreak /PERCENT INTO pc_ /PRINT = YES /TIES = MEAN.
XSAVE OUTFILE = 'data.sav'.
SORT CASES BY pc_ (D).
SELECT IF pc_ <= !lowpc.
N OF CASES 1.
RENAME VARIABLE (!var = minval).
SAVE OUTFILE = 'minval.sav' /KEEP = nobreak minval.
GET FILE='data.sav'.
SELECT IF pc_ >= (!hipc).
SORT CASES BY pc_ (A).
N OF CASES 1.
RENAME VARIABLE (!var = maxval).
SAVE OUTFILE = 'maxval.sav' /KEEP = nobreak maxval.
MATCH FILES FILE = 'data.sav'
/TABLE = 'minval.sav'
/TABLE = 'maxval.sav'
/BY = nobreak
/DROP = nobreak.
COMPUTE !var_w = MAX(MIN(!var, maxval), minval).
EXECUTE.
DELETE VARIABLES pc_ minval maxval.
DATASET CLOSE DataSet1.
EXECUTE.
!ENDDEFINE.
!winsor var = roa lowpc = 1 hipc = 1.

In order to add a suffix to your new variable name you need to use the spss macro command !concat like this:
COMPUTE !concat(!var,"_w") = MAX(MIN(!var, maxval), minval).
In order to do this for a list of variables you can use a macro loop:
define macrowithloop(vrs=!cmdend)
!do !vr !in(!vrs)
* do stuff.
COMPUTE !concat(!vr,"_w") = MAX(MIN(!vr, maxval), minval).
* other stuff.
!doend
!enddefine.
macrowithloop vars=roa leverage roe lowpc .

Related

Create tex table with list of coefficients

I want to create a tex table showing the different coefficients of an x variable when regressed on different y variables. My code is as follows:
local vars yvar1 yvar2 yvar3 etc. [there are over 100]
foreach var of local vars{
reghdfe `var' xvar, vce(cluster i.country) a(i.country)
scalar `var'_coefficient = _b[xvar]
}
Therefore, I have a list of scalars which correspond to the coefficients that I want to include in a tex table with the following format:
Variable
X Coefficient
yvar1
0.5
yvar2
-0.4
yvar3
0.7
...
...
I have tried a few ways of creating a table like this but so far to no avail. I was wondering if anyone has any ideas of how I could create such a table?
Thank you very much!
Sounds like you can use the following code. However, does not seem efficient since you'd need to input the variable names into the frmttable command. You can do this automatically with outreg2. However, you would have 100 columns. There is probably a more efficient way to present your output than a table with 100 roes.
bcuse wage2, clear
local vars hours married south sibs meduc lwage
local count = 0
foreach var of local vars {
reghdfe `var' lwage, vce(cluster urban) a(urban)
scalar `var'_coefficient = e(b)[1,1]
local count = `count' + 1
}
matrix coefficients = J(`count',1,.)
local count = 1
foreach var of local vars {
mat coefficients[`count',1] = `var'_coefficient
local count = `count' + 1
}
frmttable, statmat(coefficients) sdec(2) rtitle( "var1" \ "var2" \ "var3" \ "var4" \ "var5" \ "var6") ctitle("variables", "coeff.")

Access any structure field chosen dynamically at run time

I have a problem, so I have a huge table where some fields contain only numbers from 1-20 and I want to move the values of the fields to a new table where there are 3 fields with a name and the number (zjdc01 or zadc01).
Now I want to check the field value from the huge table and append the values to the new fields.
For Example :
CASE LS_ATLAS_DC-ZJDC01.
WHEN 1.
LS_ATLAS-ZJDC01 = LS_ATLAS_DC-ZJDC01.
LS_ATLAS-ZADC01 = LS_ATLAS_DC-ZADC01.
LS_ATLAS-ZBDC01 = LS_ATLAS_DC-ZBDC01.
WHEN 2.
LS_ATLAS-ZJDC02 = LS_ATLAS_DC-ZJDC01.
LS_ATLAS-ZADC02 = LS_ATLAS_DC-ZADC01.
LS_ATLAS-ZBDC02 = LS_ATLAS_DC-ZBDC01.
WHEN 3.
LS_ATLAS-ZJDC03 = LS_ATLAS_DC-ZJDC01.
LS_ATLAS-ZADC03 = LS_ATLAS_DC-ZADC01.
LS_ATLAS-ZBDC03 = LS_ATLAS_DC-ZBDC01.
WHEN 4.
LS_ATLAS-ZJDC04 = LS_ATLAS_DC-ZJDC01.
LS_ATLAS-ZADC04 = LS_ATLAS_DC-ZADC01.
LS_ATLAS-ZBDC04 = LS_ATLAS_DC-ZBDC01.
But this is very exhausting and I think there is another Solution but I dont know if ABAP have something for this.
Maybe some of you have a Solution or have a similiar problem which he solved.
Use ASSIGN COMPONENT name OF STRUCTURE structure TO <field_symbol>.
DATA name TYPE string. " component name
FIELD-SYMBOLS: <zjdc_xx> TYPE any,
<zadc_xx> TYPE any,
<zbdc_xx> TYPE any.
IF number BETWEEN 1 and 4.
name = |ZJDC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|. "<== ZJDC01 to ZJDC04
ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zjdc_xx>.
name = |ZADC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|. "<== ZADC01 to ZADC04
ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zadc_xx>.
name = |ZBDC{ number WIDTH = 2 ALIGN = RIGHT PAD = '0' }|. "<== ZBDC01 to ZBDC04
ASSIGN COMPONENT name OF STRUCTURE ls_atlas TO <zbdc_xx>.
<zjdc_xx> = LS_ATLAS_DC-ZJDC01.
<zadc_xx> = LS_ATLAS_DC-ZADC01.
<zbdc_xx> = LS_ATLAS_DC-ZBDC01.
ENDIF.

Lua Nested Table Getting Elements

I have a nested table like so:
t1 ={}
t1[1] = {col1=1,col2=1,col3=1,col4=1}
t1[2] = {col1=1,col2=1,col3=1,col4=1}
t1[3] = {col1=1,col2=1,col3=1,col4=1}
t1[4] = {col1=1,col2=1,col3=1,col4=1}
it's actually much larger with 250 items in t1 and 30 items per nested table so what I want to do is loop through and get sub table values like this:
for i = 2, 4 do
local width = t1[draw.ID].col1 --draw.ID is got elsewhere
end
but changing the number part of .col1 to the i part so when it loops through it gets:
t1[draw.ID].col2
t1[draw.ID].col3
t1[draw.ID].col4
I'm using Lua 5.1.
for i= 2, 4 do
local width = t1[draw.ID]["col" .. i] --draw.ID is got elsewhere
end
Ideally, col would be or would contain an array-like table or sequence. This is a much more scalable way to accomplish what you're trying to do. String concatenation ['col' .. i] to access table keys in the fashion that you'd access them as an array is costly and unnecessary, if it can be avoided. This is especially important if this is something you plan to do often and want to work quickly.
-- Elements of t1 contain tables with cols.
local t1 = {}
t1[1] = {cols = {1,1,1,1}}
t1[2] = {cols = {1,1,1,1}}
t1[3] = {cols = {1,1,1,1}}
t1[4] = {cols = {1,1,1,1}}
for i=2, 4 do
local width = t1[draw.ID].cols[i]
end
-- Elements of t1 are the cols.
local t1 = {}
t1[1] = {1,1,1,1}
t1[2] = {1,1,1,1}
t1[3] = {1,1,1,1}
t1[4] = {1,1,1,1}
for i=2, 4 do
local width = t1[draw.ID][i]
end
Edit: If it's unavoidable that you have to use table keys in the style of ['col' .. i], then the best you could do is to cache them for faster access.
-- Cache all the possible keys that you'll need.
local colkeys = {}
for i=1, 30 do colkeys[i] = 'col' .. i end
for i=2, 4 do
local width = t1[draw.ID][colkeys[i]]
end
This method is anywhere from 4 to 8 times faster than concatenating a string each time that you need to index the table. It's not the ideal solution, but it works if you're stuck with the likes of col1 to col30.

Randoms in one Array

I have six variables, that these variables have different position on the screen, I wanna put different images in these variables, hence i have an Array with the images.
misImagenes = {[1] = "rec/ro.png",[2] ="rec/az.png",[3] ="rec/ros.png",[4] ="rec/ne.png",[5] ="rec/ve.png",[6] ="rec/am.png"}
I put the elements of this Array into another Array into that have 2 different randoms, like this:
randoms = {[1] = misImagenes[math.random(1,6)],[2] = misImagenes[math.random(1,6)] }
So, I wanna put this randoms of random form, hence, i create an random of the randoms.
randomRan = randoms[math.random(1,2)]
I put the randomRan into the 6 variables, but the images of the variables are always equals.
uno = display.newImageRect(randomRan,340,280)
dos = display.newImageRect(randomRan,340,280)
tres = display.newImageRect(randomRan,340,280)
cuatro = display.newImageRect(randomRan,340,280)
cinco = display.newImageRect(randomRan,340,280)
seis = display.newImageRect(randomRan,340,280)
This variables have the randomRan, but the images are alway equals, i need that the images are differents, 2 differents images in random variables.
Thanks
It looks like what you want to do is commonly called shuffling and filtering an array.
Once you assign randoms[math.random(1,2)] to the randomRan variable, it is going to stay the same no matter what. It isn't like randomRan is going to be random each time it's used. However, if it were a function call, like randomRan(), then that would be a different case, depending on what the function did. A variable, once assigned to, generally stays the same unless changed.
math.randomseed(os.time()) -- Make sure to seed the random number generator.
local function shuffle(t)
local n = #t
while n >= 2 do
-- n is now the last pertinent index
local k = math.random(n) -- 1 <= k <= n
-- Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
local misImagenes = {"rec/ro.png", "rec/az.png", "rec/ros.png", "rec/ne.png", "rec/ve.png", "rec/am.png"}
local randomImages = {}
-- Make a copy of misImagenes for randomImages.
for i, v in ipairs(misImagenes) do randomImages[i] = v end
-- Shuffle the new array. This will randomize the order of its contents.
shuffle(randomImages)
-- Since you want only two unique images for a total of six rectangles,
-- we'll have to duplicate and overwrite the other four, randomly.
for i = 1+2, 6 do
randomImages[i] = randomImages[math.random(1, 2)]
end
-- Now to filter the array with newImageRect.
for i=1, #randomImages do
randomImages[i] = display.newImageRect(randomImages[i], 340, 280)
end
-- randomImages now contains all of your randomized image rectangles.
The shuffle algorithm was borrowed from here to show an example of how this could work.
If you are doing
var1 = randomRan
var2 = randomRan
then var1 and var2 will have the same value - randomRan does not get recomputed each time its evaluated. YIf that is what you want, you can repeat the expression you used to initialize randomRan:
var1 = randoms[math.random(1,2)]
var2 = randoms[math.random(1,2)]
and if you want to avoid retyping that ocmplex expression, you can encapsulate it in a function:
--Return a random image
local function randomRan()
return randoms[math.random(1,2)]
end
var1 = randomRan()
var2 = randomRan()

Stata: multiplying each variable of a set of time-series variables with the corresponding variable of another set

Being fairly new to Stata, I'm having a difficulty figuring out how to do the following:
I have time-series data on selling price (p) and quantity sold (q) for 10 products in a single datafile (i,e., 20 variables, p01-p10 and q01-q10). I am strugling with appropriate stata command that computes sales revenue (pq) time-series for each of these 10 products (i.e., pq01-pq10).
Many thanks for your help.
forval i = 1/10 {
local j : display %02.0f `i'
gen pq`j' = p`j' * q`j'
}
A standard loop over 1/10 won't get you the leading zero in 01/09. For that we need to use an appropriate format. See also
#article {pr0051,
author = "Cox, N. J.",
title = "Stata tip 85: Looping over nonintegers",
journal = "Stata Journal",
publisher = "Stata Press",
address = "College Station, TX",
volume = "10",
number = "1",
year = "2010",
pages = "160-163(4)",
url = "http://www.stata-journal.com/article.html?article=pr0051"
}
(added later) Another way to do it is
local j = string(`i', "%02.0f")
That makes it a bit more explicit that you are mapping from numbers 1,...,10 to strings "01",...,"10".

Resources