So I have this information:
Based on this information, I am required to produce a SAS Dataset with an additional date variable- YearMonth (YYYYMM)
We can populate the values inside the variable randomly.
The list of variables can be 2, 10 or 50.(var1 , var2, var3.....)
HAVE:
/*HAVE*/
data have;
infile datalines missover ;
input varname :$8. type length format $8.;
datalines;
var1 1 8
var2 1 8
var3 2 8
var4 2 1
var5 1 8
;run;
WANT:
YEARMONTH | VAR1 | VAR2 | VAR3 | VAR4 | VAR5 |
202002 | 1 | 2 | A | B | 5 |
202003 | 3 | 3 | C | D | 6 |
202004 | 4 | 4 | E | F | 7 |
202005 | 5 | 5 | G | H | 8 |
Please advise.
Thank You
If you want to define variables from metadata then just use some type of code generation.
For example you could generate a series of ATTRIB statements.
filename code temp;
data _null_;
set have ;
file code ;
length name $51 ;
name=nliteral(varname);
put 'attrib ' name 'length=' @;
if type=2 then put '$' @;
put length @;
if not missing(format) then put format= @;
put ';';
run;
You could then use those statements inside a data step to define the variables.
data want;
do yearmonth = '202002','202003','202004';
%include code / source2;
output;
end;
run;
Please explain the logic that results in 202002 in the first row, 202003 in the second row, and so on. Thanks!
If you want to define variables from metadata then just use some type of code generation.
For example you could generate a series of ATTRIB statements.
filename code temp;
data _null_;
set have ;
file code ;
length name $51 ;
name=nliteral(varname);
put 'attrib ' name 'length=' @;
if type=2 then put '$' @;
put length @;
if not missing(format) then put format= @;
put ';';
run;
You could then use those statements inside a data step to define the variables.
data want;
do yearmonth = '202002','202003','202004';
%include code / source2;
output;
end;
run;
Thank You very much @Tom
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.