BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I have a data set with wide structure: for each month there is information of multiple variables .

Some of the variables are numeric and some are char.

For some char variables there is user defined format .

I want to convert the structure from wide to Long .

The wanted structure (Long ) will show me information of multiple variables for each month.

After the transpose I see strange values

Why did it happen?

Is it because  there is mix of numeric and char vars before proc transpose?

Should I convert the numeric to char before do trapspose?

Ronein_0-1684070219206.png

 

 

4 REPLIES 4
yabwon
Onyx | Level 15

How about doing something like this:

data have;
input month a b $ c d $;
cards;
1 11 A 100 ABC
2 22 B 200 DEF
3 33 C 300 GHI
4 44 D 400 JKL
;
run;
proc print;
run;

data want (compress=char);
  set have;

  array NN[*] a-Numeric-d;
  array CC[*] a-Character-d;

  length varName varFmnt $ 32 valNum 8 valChar $ 200;

  call missing(valNum,valChar);
  do _N_=1 to dim(NN);
    varName=vname(NN[_N_]);
    varFmnt=vformat(NN[_N_]);
    valNum=NN[_N_];
    output;
  end;

  call missing(valNum,valChar);
  do _N_=1 to dim(CC);
    varName=vname(CC[_N_]);
    varFmnt=vformat(CC[_N_]);
    valChar=CC[_N_];
    output;
  end;

  keep month varName varFmnt valNum valChar;
  format varName varFmnt valNum valChar;
run;
proc print;
run;

?

Numeric values stay in numeric, character in character, and format value is stored.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ronein
Meteorite | Level 14
I would like to know please why did it happen that after proc transpose (wide to long) there are such values?
By the way,these values are not null values. May anyone tell what is this symbol of this values?
ballardw
Super User

@Ronein wrote:

Hello

 

After the transpose I see strange values

Why did it happen?

 

Ronein_0-1684070219206.png

 

 


Which ones are "strange values" we do not have your source data set. We do not know anything about the data or the meaning of any values.

You don't even show the code you used.

Transpose does not work well with Numeric and Character variables as VAR variables.

 

Whats wrong with a report? You keep trying to create obnoxious data sets. If the purpose is "show me information of multiple variables for each month" sounds like a report is more likely useful than a data set. Plus the picture you show does not appear to have any "Month" information involved.

 

Getting multiple Col for any given "_name_" value means that if you used any sort of by to group the data then the by variable(s) have multiple identical values and your approach to "long" may not have been prepared correctly.

 

Show starting data in a usable form.

PaigeMiller
Diamond | Level 26

Agreeing with @ballardw 

 

You put yourself in difficult situations by continually trying to work with data sets that are not in a format that SAS was designed to handle. In this case, you seem to have a different variable in each row, and values for that variable going across the rows. This is the EXACT OPPOSITE of what it is supposed to be for easy SAS coding, in which variables are columns and the values of the variable are going down the columns. You need to learn to recognize this and avoid it like the plague.

 

Do not create data sets like this, or like the one with the exact same problem in an earlier thread. Recognize that this a bad habit pattern we see from you, and begin trying to correct this bad habit pattern.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 563 views
  • 0 likes
  • 4 in conversation