Issue in MAKE command - spss

I am trying to restructure one .sav file using VARSTOCASES command and i got one syntax from IBM site,but i didn't get any examples for the same so i tried with some variables but its not working,Can anyone help for this
Actual Syntax from IBM Site
VARSTOCASES
/MAKE new variable ["label"] [FROM] varlist [/MAKE ...]
[/INDEX = {new variable ["label"] }]
{new variable ["label"] (make variable name) }
{new variable ["label"] (n) new variable ["label"](n) ...}
[/ID = new variable ["label"]]
[/NULL = {DROP**}]
{KEEP }
[/COUNT=new variable ["label"]]
[/KEEP={ALL** }] [/DROP=varlist]
{varlist}
My code
GET
FILE='D:\SPSStoCSV\FileFormator\bin\FILES\Argentina\Temp.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
VARSTOCASES
/MAKE new variable VariableName1 [FROM] varlist [/MAKE Purchp6m_2 Purchp6m_4]
[/INDEX = {new variable VariableName1 }]
{new variable VariableName1 (make variable name) }
{new variable VariableName1 (3) new variable VariableName1(3) ...}
[/ID = new variable VariableName1]
[/NULL = {DROP**}]
{KEEP }
[/COUNT=new variable VariableName1]
[/KEEP={ALL** }] [/DROP=varlist]
{varlist}.
SAVE TRANSLATE OUTFILE='C:\Users\10522\Desktop\Temp.csv'
/TYPE=CSV
/ENCODING='UTF8'
/MAP
/REPLACE
/FIELDNAMES
/CELLS=VALUES.
here i want to keep all the variables except these 2(Purchp6m_2 Purchp6m_4)in KEEP command,Here i have some 150 variables so instead of keeping all the 148 variable names in the KEEP command i am trying like All** and DROP command, but this is giving all data without restructuring any of the variables.I am trying to avoid adding all the remaining variables in the KEEP command.

First, remove the square and the curly brackets [, ], { and }. They are used on IBM website only to signal optional arguments and lists of variables; they are not part of the syntax itself.
Second, you cannot use spaces in your variable names, so new variableshould be new_variable
Third, you are using new_variable multiple times, which is wrong. you need to assign actual names to those variables.
Your code needs to be something like this: (although it is not very clear neither to me, and probably nor to you, why you need all the optional arguments):
GET
FILE='D:\SPSStoCSV\FileFormator\bin\FILES\Argentina\Temp.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
VARSTOCASES
/MAKE Purchp6m "Purchp6m" FROM Purchp6m_2 Purchp6m_4
/INDEX = index_variable "Index variable"
/ID = id_variable "Variable ID"
/NULL = KEEP
/COUNT=count_variable "Count VAriable"
/KEEP=ALL.
EXECUTE.
SAVE TRANSLATE OUTFILE='C:\Users\10522\Desktop\Temp.csv'
/TYPE=CSV
/ENCODING='UTF8'
/MAP
/REPLACE
/FIELDNAMES
/CELLS=VALUES.
KEEP=ALL is the default, so you don't actually need to specify it

Related

Lua - Why don't Global variables need declarations?

According to Lua's manual
Global variables do not need declarations. You simply assign a value to a global variable to create it. It is not an error to access a non-initialized variable; you just get the special value nil as the result
I think declaration is good, it makes thing more manageable. Why did Lua skip declarations for Global variables? I guess they have a good reason but I don't know why.
What if I make this mistake
-- Activation Code
function TestLoco:OnActivate()
self.MyGlobal = "ABC"; --Init the global variable
end
-- Run every tick
function TestLoco:OnTick()
self.MyGIobaI = "BCD"; --Edit the global variable at every tick, but I mistake 'I' with 'l'
end
-- Call when I click the mouse
function TestLoco:OnClick()
Debug.Log(self.MyGlobal); --This print "ABC", but It should be "BCD", what if I don't notice this?
end
Because Lua has no classes. self.MyGlobal is not a global variable, it is a field in the table passed via the self parameter. The syntax is equivalent to self["MyGlobal"]. For a "true" global variable assignment (e.g. x = 0), it is equivalent to _G["x"] = 0, where _G is the global environment table.
Since Lua has no notion of classes, the type of self is simply a table. The syntax you use for specifying a "method" is just a syntactic shortcut to this:
TestLoco.OnActivate = function(self)
self["MyGlobal"] = "ABC";
end
It's just a function that assigns a field in a table. It could be called with potentially any table, hence it cannot verify the table actually should have that field.
However, Lua offers some pretty good run-time customisable checking via metatables. If you build a table specifying the "class" of a table, you can assign a metatable to every table that checks the assignment each time if it assigns to a field that you actually "declared".

Stata: using foreach to rename numeric variables

I have a large dataset where subsets of variables have been entered with the same prefix, followed by an underscore and some details. They are all binary YN and the variables are all doubles. For example, I have the variables onsite_healthclinic and onsite_CBO where values can only be 1 or 0.
I want to rename them all according to the question they are on the survey I'm working off of (so the above variables would become q0052_healthclinic and q0052_CBO), but if I use the code below using substr I (obviously) get type mismatch:
foreach var in onsite_healthclinic onsite_CBO {
local new = substr(`var', 8, .)
rename `new' q0052_`new'
}
My question is, is there another command other than substr that I can use so that I don't have to either a) convert all of the variables to strings first; or b) rename them all manually (there are ~20 in each subset, so while doable, it's a waste of time).
There is no need for a loop here at all. Although the essential answer is one line long I give here a complete, self-contained answer.
clear
set obs 1
foreach v in onsite_healthclinic onsite_CBO {
gen `v' = 1
}
rename onsite_* q0052_*
describe, fullnames
This answer implies that you've not studied the help under rename groups.
Will this work?
foreach var in onsite_healthclinic onsite_CBO {
local new = substr("`var'", 8, .)
rename onsite_`new' q0052_`new'
}
I added quotes around the call to the local var in the substr function and added onsite_ to the rename and that seemed to work.

Get global environment in lua package

At the begining of some lua package files, sometimes there will be the line local base = _G or local base = ....
What's the benefits for doing this?
What's the differences between these two lines?
For the first question, you can refer: Why make global Lua functions local?
For your second one,
What's the differences between these two lines?
When you do local base = _G, you are assigning base to be a synonym for the global environment table. On the other hand, in the statement local base = ...; the ... refer to vararg feature of lua.
It can be shown in better detail with the following example:
local a = {...}
for k, v in pairs(a) do print(k, v) end
and then, executing it as follows:
─$ lua temp.lua some thing is passed "here within quotes"
1 some
2 thing
3 is
4 passed
5 here within quotes
As you see, ... is just a list of arguments passed to the program. Now, when you have
local base = ...
lua assigns the first argument to the variable base. All other parameters will be ignored in the above statement.

How to define variable that named by function?

I need to create many variables, and I want to do declare these variable's name by function. In like this:
function DefineVariable(VariableName)
return VariableName = 123 end
What i want from the function is:
DefineVariable(weg423)
-- Result: weg423 = 123
But it was not easy as I thought... That function does not worked. I want to know how to turn it into working function.
And, could i receive an answer about this too?
a = "blahblah"
...somehow...
DefineVarByValue(a)
-- Result: blahblah = 123
Ah and one more question. how to change 'table variable' to 'string or number variable'? (i mean the 'number expected, got table' error...)
All simple types in Lua are passed by value, so your DefineVariable function gets the current value of the variable weg423 and not the name of it, which means you can't change the value of weg423 from the function.
If this is a global variable, then you can pass the name of that variable (for example, DefineVariable('weg423')) and the function can then change the value of that variable by referencing it as a key in _G or _ENV table (_G[VariableName] = whatever).
I still don't see a point in doing it this way. There is nothing wrong with using weg423 = 123.

Variable names in table field not working

I came across a problem while writing some code up for a game. It seems I can't use variables in statements like;
local Username = "Cranavvo"
game.Players.Username:BreakJoints() -- Kills the player
And the output is telling me "No such user as 'Username'" which should be "Cranavvo".
From Lua PiL on tables
To represent records, you use the field name as an index. Lua supports
this representation by providing a.name as syntactic sugar for
a["name"].
A common mistake for beginners is to confuse a.x with a[x]. The first
form represents a["x"], that is, a table indexed by the string "x".
Therefore, when you try:
game.Players.Username:BreakJoints()
Lua interprets it as:
game["Players"]["Username"]:BreakJoints()
which ofcourse is wrong. If you want to use varying name as index for a table, use them like this:
local foo = "Cranavvo"
game.Players[foo]:BreakJoints()
But to be mentioned is that the Player class do not have a BreakJoints method, you have to get the character model with help of the .Character attribute like this:
local foo = "Cranavvo"
game.Players[foo].Character:BreakJoints()
Also to be mentioned is that if the player with that name does not exist the code will break, and also that the character can be null, in which case it also breaks. Thus you need to add some error handling. Like this:
local foo = "Cranavvo"
local Player = game.Players:findFirstChild(foo)
if Player ~= nil and Player.Character ~= nil then
Player.Character:BreakJoints()
end
The correct way to do this in roblox is this:
local Username = "Cranavvo"
local p = game.Players:FindFirstChild(Username)
if(p ~= nil) then
if(p.Character ~= nil) then
p.Character:BreakJoints()
end
end
Double check if the user really exists at the time your code gets executed.
Also it should be:
game.Players.Username:BreakJoints()
EDIT:
I misread what you wanted to do:
in
...Players.Username
lua interprets Username as a named index and does not use the Username variable declared beforehand.
If you want to access an array variable with a dynamic name you can do it like this:
game.Players[Username]:BreakJoints()
additionally in roblox you could just use the following:
game.Players:GetPlayerByID(userId):BreakJoints()
Variables are very confusing in Lua sometimes.
For example, there are global and local variables.
Local variables are variables that can be forgotten/erased after the operation ends: local x = 2
Global variables are variables that stay within the game/application unforgotten, this is good with high scores and other neat things. x = 2 (Notice there isn't a "local" statement)

Resources