Hello
I need to create a data set from external macro varaibles m0,m1,m2,m3,m4,m5,m6
I have 3 questions please:
1-Why data set ttbl is not created well and I get null values
2-I need to calculate a new column called "DateName".
For each value of YYMM I need to create a value of 01 of month.
foe example:
for 1906 I need to get '01JUN2019'd
for 1905 I need to get '01May2019'd
for 1904 I need to get '01APR2019'd
for 1903 I need to get '01MAR2019'd
for 1902 I need to get '01FEB2019'd
for 1901 I need to get '01JAN2019'd
3-For each value of DateName I need to create a macro varaible.
for example:
macro varaible R0 will get value '01JUN2019'd
macro varaible R1 will get value '01May2019'd
macro varaible R2 will get value '01APR2019'd
and so on
%let m0=1906;
%let m1=1905;
%let m2=1904;
%let m3=1903;
%let m4=1902;
%let m5=1901;
%let m6=1812;
%put &m0 &m1 &m2 &m3 &m4 &m5 &m6;
Data ttbl;
Input YYMM;
cards;
&m0
&m1
&m2
&m3
&m4
&m5
&m6
;
run;
/*Why is it not created well???*/
@PeterClemmensen wrote:
And here is a workaround
http://support.sas.com/kb/43/295.html
This makes me sad.
A better work-around would be to design a simpler/better data flow for this situation, which doesn't use macro variables in the first place. This is especially important for new SAS users to learn, coming up with an efficient data flow — don't use macro variables where they aren't necessary.
I agree.
@Ronein wrote:
Hello
I need to create a data set from external macro varaibles m0,m1,m2,m3,m4,m5,m6
I have 3 questions please:
1-Why data set ttbl is not created well and I get null values
2-I need to calculate a new column called "DateName".
For each value of YYMM I need to create a value of 01 of month.
foe example:
for 1906 I need to get '01JUN2019'd
for 1905 I need to get '01May2019'd
for 1904 I need to get '01APR2019'd
for 1903 I need to get '01MAR2019'd
for 1902 I need to get '01FEB2019'd
for 1901 I need to get '01JAN2019'd
Seems like an ideal situation to re-design the data flow into your SAS data set, for example instead of creating macro variables, put the values such as 1906 directly into a SAS data set where they belong. It's probably no more work putting them directly into a SAS data set than it is to create macro variables. Also possible: use some other type of input storage, such as a CSV file or plain old text file, instead of macro variables.
As far as turning 1906 into '01JUN2019'd, this could be easily done by utilizing informats or date functions, such as MDY().
The question is silly on so many levels. Why do you have macro variables? Why do you want macro variables? Why do have a bunch of macro variables? Why do you have only two digits for the year?
But let's just answer it.
Let's assume you have M0 to M6 (and you KNOW that they exist and that always contain valid YYMM strings). Just use a data step to loop from 0 to 6 and pull in the macro variable's value and convert it to a date and write it back into new macro variables.
Do you want the VALUE of the dates?
data _null_;
do i=0 to 6;
date=input(symget(cats('M',i))||'01',yymmdd6.);
call symputx(cats('R',i),date);
end;
run;
Or the string with the quotes and D suffix like in your example:
call symputx(cats('R',i),cats("'",put(date,date9.),"'d"));
Hello All,
I am trying also to create the data set but I get null values.
I have tried to read the document but I cannot understand what should I do.
%let m0=1906;
%let m1=1905;
%let m2=1904;
%let m3=1903;
%let m4=1902;
%let m5=1901;
%let m6=1812;
%put &m0 &m1 &m2 &m3 &m4 &m5 &m6;
Data ttbl;
Input YYMM;
cards;
&m0
&m1
&m2
&m3
&m4
&m5
&m6
;
run;
@DaveStar wrote:
Hello All,
I am trying also to create the data set but I get null values.
I have tried to read the document but I cannot understand what should I do.
%let m0=1906; %let m1=1905; %let m2=1904; %let m3=1903; %let m4=1902; %let m5=1901; %let m6=1812; %put &m0 &m1 &m2 &m3 &m4 &m5 &m6; Data ttbl; Input YYMM; cards; &m0 &m1 &m2 &m3 &m4 &m5 &m6 ; run;
@DaveStar this was explained earlier in this thread by @PeterClemmensen .
@Ronein and @DaveStar you can't use macro variables as input data under CARDS (or DATALINES);
Sas reads it as is, '&m0' instead as '1906'.
The macro variables are to be used in the sas code only. See @Tom's post or next simple code. Try it.
%let m0 = 1906;
%let m1 = 1905;
...
data test;
m0 = &m0.01;
date0 = input(m0, yymmdd8.); ;
call symput('dt0', put(date0, date9.);
output;
m1 = &m1.01;
date1 = input(m1, yymmdd8.);
call symput('dt1', put(date1, date9.);
output;
run;
%put &dt0 &ddt1;
I will try to do it.
Is it forbidden to ask questions regarding homework...?Lol....
No worries...it is not Homework.
Thanks
No its not forbidden to ask questions about homework, but normally we expect people to try the homework themselves and show us what they tried.
However, when two different people have the exact same data and requirements, we get suspicious, this sure seems like homework. If it is homework, it is a terrible assignment and if an instructor said to do this with macro variables as shown, the instructor ought not to be an instructor.
Of course, I agree for every word.
Before asking a question I am trying to solve it by myself.
Moreover, I am trying to learn from more experienced people.
Thank you for your advice anyway.
And....take life more easy. It is still not so bad 🙂
Same answer, only use a real dataset name and an output statement.
data want;
length i 8 yymm 8 ;
do i=0 to 6;
yymm=input(symget(cats('m',i)),32.);
output;
end;
run;
Homework problem or multiple accounts?
@Ronein wrote:
Hello
I need to create a data set from external macro varaibles m0,m1,m2,m3,m4,m5,m6
I have 3 questions please:
1-Why data set ttbl is not created well and I get null values
2-I need to calculate a new column called "DateName".
For each value of YYMM I need to create a value of 01 of month.
foe example:
for 1906 I need to get '01JUN2019'd
for 1905 I need to get '01May2019'd
for 1904 I need to get '01APR2019'd
for 1903 I need to get '01MAR2019'd
for 1902 I need to get '01FEB2019'd
for 1901 I need to get '01JAN2019'd
3-For each value of DateName I need to create a macro varaible.
for example:
macro varaible R0 will get value '01JUN2019'd
macro varaible R1 will get value '01May2019'd
macro varaible R2 will get value '01APR2019'd
and so on
%let m0=1906; %let m1=1905; %let m2=1904; %let m3=1903; %let m4=1902; %let m5=1901; %let m6=1812; %put &m0 &m1 &m2 &m3 &m4 &m5 &m6; Data ttbl; Input YYMM; cards; &m0 &m1 &m2 &m3 &m4 &m5 &m6 ; run; /*Why is it not created well???*/
@Ronein and @DaveStar a good understanding about SAS dates may assist you in the future.
Here is a link may assist both of you.
http://v8doc.sas.com/sashtml/lrcon/zenid-63.htm
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.