- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;
Now i need to create a macro &x.using the above dataset
data two;
input &x;
datalines;
a b c
;
run;
macro &x should resolve like this - var1 $10 var2 $9 var3 $11
I couldn't get the slightest idea how to do this. Can someone help with this?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your original post stated that you want to create an INPUT statement using the macro variable &X. As others have mentioned, that gives you a program that does the wrong thing.
However, you later clarified that saying that you want the macro variable to be used in a LENGTH statement. That is quite possible. After creating the data set ONE, you would use:
proc sql;
select variable into : x separated by ' ' from one;
quit;
%put &X;
Now you have the macro variable X with the desired value, and you can experiment with using it any way that you see fit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First of all, this really isn't clear. You mention you want a "macro", but it seems like you really want a "macro variable". "Macro" and "macro variable" are not the same thing. Please confirm you really want a "macro variable".
Next, even if you get a macro variable named &X that has the value you want, it will not produce a meaningful result in DATA TWO if you run it. You need to make sure that the macro variable, when replaced by its actual value, produces legal valid working SAS code (and this isn't working properly).
If I am understanding you properly, in DATA TWO, you want the second line to say (after replacing &X with its value)
input var1 $10 var2 $9 var3 $11;
but this will not work. It will read the data incorrectly. These are the things that you have to think about and fix before you try to create a macro variable and use it. What should this INPUT statement say? If you had to write it without macro variables, show me an INPUT statement that will actually work here and produce the desired result. To me, creating code that works properly without macro variables is a MANDATORY (not optional) first step.
What is the desired result from DATA TWO anyway? You don't tell us.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, I need to create a macro variable &x. and its should resolve like this
input var1 $10 var2 $9 var3 $11
I just gave some random values for those variables. All I need is to create a macro variable and it should resolve like mentioned above. Sorry for the confusion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
But as I said, this INPUT statement will read the data incorrectly.
What should the INPUT statement say (without macro variables, using only SAS data step code) so that it will read the data correctly?
I really want you to understand the thought process here. Your macro variable, when it is replaced by its value, must produce working SAS code. You need to have that working SAS code before you can create a macro variable. You can't just create any text string in your macro variable &X and expect it to do what you want.
If you don't have working SAS code to start with in DATA TWO, then using a macro variable will fail as well.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, Now I understood your point.
I need dataset two like this based on dataset one
data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;
crreate a macro variable &x.
length &x.; should resolve length var1 $10 var2 $9 var3 $11;
data two;
length var1 $10 var2 $9 var3 $11;
input var1 $ var2 $ var3 $;
datalines;
a b c
;
run;
Hope I am clear this time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Data Two was defined as (taken from the original post):
data two;
input &x;
datalines;
a b c
;
run;
Then suppose the macro variable is assigned by %LET, as in:
%let x = var1 $10 var2 $9 var3 $11 ;
data two;
input &x;
datalines;
a b c
;
run;
then it will resolve to:
data two;
input var1 $10 var2 $9 var3 $11;
datalines;
a b c
;
run;
and it will run without errors.
If I understand correctly, the target is to create the macro variable X in data One,
then the code to do it is:
data one;
infile datalines eov=eof;
length x $30; retain x;
input variables $1-8;
x = catx(' ',x,variables);
call symputx('x',x);
datalines4;
var1 $10
var2 $9
var3 $11
;;;;
run;
%put x= &x;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@helpmedoubts wrote:
data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;
Now i need to create a macro &x.using the above dataset
data two;
input &x;
datalines;
a b c
;
run;
macro &x should resolve like this - var1 $10 var2 $9 var3 $11
I couldn't get the slightest idea how to do this. Can someone help with this?
This is not possible; the data in dataset one can't be used to create working code. You need to fix dataset one first, before you can use it to create the macro variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your original post stated that you want to create an INPUT statement using the macro variable &X. As others have mentioned, that gives you a program that does the wrong thing.
However, you later clarified that saying that you want the macro variable to be used in a LENGTH statement. That is quite possible. After creating the data set ONE, you would use:
proc sql;
select variable into : x separated by ' ' from one;
quit;
%put &X;
Now you have the macro variable X with the desired value, and you can experiment with using it any way that you see fit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much. It worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you want to use metadata (ONE) to generate code then make sure the metadata has the information you need.
Your example seems to have mashed two variables into into one. The name of the variable and the length of the variable. I say length instead of format because the values you list do not have the period needed to be format specifications, but are valid syntax for setting the length of a variable in a LENGTH statement.
You also need to know what code you want to generate. Your proposed code does not look right. Do you really want VAR1 to be the single character in column 10 of the input line? And VAR2 to the single character in column 9?
So assuming your values are lengths you could modify your code like this to do something reasonable. (It is still not clear if it is what you want to do.)
data one;
varnum+1;
input variable :$32. length :$6.;
datalines;
var1 $10
var2 $9
var3 $11
;
proc sql noprint;
select catx(' ',variable,length)
into :lengths separated by ' '
from one
order by varnum
;
quit;
data two;
length &lengths;
input (_all_) (+0);
datalines;
a b c
;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you. This code worked. The values I gave in the original dataset were single characters. I just gave them as an example. My main doubt was how to create a macro variable. Thanks a lot for the code. it worked perfectly. But I couldn't understand this line
input (_all_) (+0);
Could you please explain this?
I never saw _all_ in input statement. It's very interesting. Learning something new everyday from this community.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
_ALL_ is a variable list. Just like A B C or VAR1-VAR2 or _NUMERIC_ or _CHARACTER_.
The parentheses are used when specifying a list of variables followed by a list of informats in a INPUT (or formats in a PUT) statement.
The +0 is a cursor movement command that will move the cursor by zero character positions. You just need it so the list of informats is not empty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank You!