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

I have a dataset with only 1 variable. The number of rows can be different day by day.

 

How it might look today:

FILE_NM
POLICY.csv
CUSTOMER.csv
PRODUCT.csv

 

How it might have looked yesterday:

FILE_NM
POLICY.csv
CUSTOMER.csv
PRODUCT.csv
COMMISSION.csv

 

I want to identify how many rows there were, and then create 1 variable per row.

 

Yesterday the variables should have been:

file_nm1 = 'POLICY.csv'

file_nm2 = 'CUSTOMER.csv'

file_nm3 = 'PRODUCT.csv'

file_nm4 = 'COMMISSION.csv'

 

Flexibility is important. If there's more input rows, there should be more variables.

 

Any suggestions on how I can accomplish this?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input FILE_NM $20.;
datalines;
POLICY.csv
CUSTOMER.csv
PRODUCT.csv
;

data temp;
    set have;
    x=cats('file_nm', _N_);
run;

proc transpose data=temp out=want(drop=_NAME_);
    id x;
    var FILE_NM;
run;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input FILE_NM $20.;
datalines;
POLICY.csv
CUSTOMER.csv
PRODUCT.csv
;

data temp;
    set have;
    x=cats('file_nm', _N_);
run;

proc transpose data=temp out=want(drop=_NAME_);
    id x;
    var FILE_NM;
run;
EinarRoed
Pyrite | Level 9
Perfection. Thank you. 🙂
EinarRoed
Pyrite | Level 9

Is there also a flexible way to load each variable into a macro variable, such as by working 'call symput' into your code?

 

&file_nm1

&file_nm2

etc...

 

I only managed to do it in a very manual/clunky way.

PeterClemmensen
Tourmaline | Level 20

Show me your clunky approach?

EinarRoed
Pyrite | Level 9

Opened the 'want' dataset and used VARNUM() to check if 'filename1' existed (by checking if the returned position was >0). If yes then call symput for filname1. Then the same manually for filename 2-15 (there likely won't ever be more than 15 rows/variables).

PeterClemmensen
Tourmaline | Level 20

Probably easier to do while creating the TEMP data set like this

 

data have;
input FILE_NM $20.;
datalines;
POLICY.csv
CUSTOMER.csv
PRODUCT.csv
;

data temp;
    set have;
    call symput(cats('file_nm', _N_), FILE_NM);
    x=cats('file_nm', _N_);
run;

%put &file_nm1.;
%put &file_nm2.;
%put &file_nm3.;

proc transpose data=temp out=want(drop=_NAME_);
    id x;
    var FILE_NM;
run;
EinarRoed
Pyrite | Level 9

Most definitely. Thanks again. Smiley Very Happy

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1220 views
  • 2 likes
  • 2 in conversation