BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
david27
Quartz | Level 8

So I have this information:

  • Name of the variable
  • Type( Character / Numeric)
  • Length
  • Format

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:

YEARMONTHVAR1VAR2VAR3VAR4VAR5
20200212AB5
20200333CD6
20200444EF7
20200555GH8

 

 

Please advise.

 

Thank You

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Please explain the logic that results in 202002 in the first row, 202003 in the second row, and so on. Thanks!

--
Paige Miller
Tom
Super User Tom
Super User

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;
david27
Quartz | Level 8

Thank You very much @Tom 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 303 views
  • 0 likes
  • 3 in conversation